import { getInfo, logout } from '@/api/auth/authUserApi'
import {
  getCompanyId,
  getHostName,
  getUserId,
  setCompanyId,
  setDeptId,
  setDevModel, setInChina,
  setLanguage,
  setLat,
  setLng,
  setToken,
  setUserIconUrl,
  setUserId,
  setUserName
} from '@/util/storage'
import { hex_md5 } from '@/util/md5'
import { getBrowser, getOsInfo } from '@/util/hardInfo'
import { listRole } from '@/api/iot/iotUserRoleApi'
import { login } from '@/api/iot/iotTokenApi'

const state = {
  roles: []
}

const mutations = {
  SET_ROLES: (state, roles) => {
    state.roles = roles
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { userName, password, loginType, ticket } = userInfo
    const { browserType, browserVersion } = getBrowser()
    const { systemType, systemVersion } = getOsInfo()
    return new Promise((resolve, reject) => {
      login({
        userName: userName.trim(),
        password: hex_md5(password),
        loginType: loginType,
        systemVersion: systemVersion,
        systemType: systemType,
        browserType: browserType,
        browserVersion: browserVersion,
        ticket: ticket,
        companyId: getCompanyId(),
        domainName: getHostName()
      }).then(res => {
        const { code, data, msg } = res
        if (code === '200') {
          // if (parseInt(getCompanyId()) !== data.companyId) {
          //   reject('企业版禁止跨公司访问')
          // }

          setCompanyId(data.companyId)
          setToken(data.accessToken)
          setDeptId(data.deptId)
          setUserId(data.userId)
          setUserName(data.userName)
          setLanguage(data.language)
          setUserIconUrl(data.iconUrl)
          setLat(data.lat)
          setLng(data.lng)
          setInChina(data.inChina)

          resolve()
        } else {
          reject(msg)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  listRole({ commit }) {
    return new Promise((resolve, reject) => {
      getInfo({
        userId: getUserId()
      }).then(res => {
        const { code, data } = res
        if (code === '200') {
          setCompanyId(data.companyId)
          setDeptId(data.deptId)
          setUserId(data.userId)
          setUserName(data.userName)
          setLanguage(data.language)
          setUserIconUrl(data.iconUrl)
          setLat(data.lat)
          setLng(data.lng)
          setDevModel(data.devModel)
        }
      })
      listRole({
        userId: getUserId()
      }).then(res => {
        const { data, code } = res
        if (code === '200') {
          // roles must be a non-empty array
          if (data.length === 0) {
            reject('你无权登录此系统')
          }
          const roles = []
          data.forEach(item => {
            roles.push(item.roleId)
          })
          commit('SET_ROLES', roles)
          resolve()
        }
      })
    })
  },

  // user logout
  logout({ commit, dispatch }) {
    return new Promise((resolve, reject) => {
      logout({}).then(() => {
        commit('SET_ROLES', [])
        setToken(null)
        setUserId(null)
        setUserName(null)
        dispatch('tagsView/delAllViews', null, { root: true })
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
